#! /usr/bin env python
# coding:utf-8
import time

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
import logging
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

service = Service(executable_path="E:\\chromedirvers\\chromedriver.exe")
browser = webdriver.Chrome(service=service)

# 等待浏览器加载10秒
# browser.implicitly_wait(10)
wait = WebDriverWait(browser, 10)

# init log
__logger = logging.getLogger('jdlog')
__logger.setLevel(logging.INFO)
fh = logging.FileHandler("detail.csv")
formatter = logging.Formatter('%(message)s')
fh.setFormatter(formatter)
__logger.addHandler(fh)

filename = './logs/itemurl-3.txt'

def getfuselageMemoryText():
    try:
        # 选择商品介绍标签
        tab1 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]/div[1]/ul/li[1]')))
        tab1.click()
        time.sleep(2)
        fuselageMemoryText = browser.find_element(By.XPATH, '//*[@id="detail"]//li[contains(text(),"机身内存")]').text
        # fuselageMemoryText = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]//li[contains(text(),"机身内存")]'))).text
        if '' == fuselageMemoryText:
            fuselageMemoryText = '未获取'
        else:
            fuselageMemoryArr = fuselageMemoryText.split('机身内存：')
            if len(fuselageMemoryArr) <= 1:
                fuselageMemoryText = '未获取'
            else:
                fuselageMemoryText = fuselageMemoryArr[1]
    except Exception as e:
        fuselageMemoryText = '未获取'
    return fuselageMemoryText

def getrunningMemoryText():
    try:
        # 选择商品介绍标签
        tab1 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]/div[1]/ul/li[1]')))
        tab1.click()
        time.sleep(2)
        runningMemoryText = browser.find_element(By.XPATH, '//*[@id="detail"]//li[contains(text(),"运行内存")]').text
        # runningMemoryText = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]//li[contains(text(),"运行内存")]'))).text
        if '' == runningMemoryText:
            runningMemoryText = '未获取'
        else:
            runningMemoryArr = runningMemoryText.split('运行内存：')
            if len(runningMemoryArr) <= 1:
                runningMemoryText = '未获取'
            else:
                runningMemoryText = runningMemoryArr[1]
    except Exception as e:
        runningMemoryText = '未获取'
    return runningMemoryText

with open(filename, 'r', encoding='utf-8') as logfile:
    for detailurl in logfile:
        if '--' not in detailurl:
            urlinfo = detailurl.split('item.jd.com/')
            if len(urlinfo) <= 1:
                continue
            detailfile = urlinfo[1]
            sku = detailfile.split('.')[0]
            try:
                browser.get(detailurl)
                # 默认选择规格标签
                tab2 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]/div[1]/ul/li[2]')))
                tab2.click()
                time.sleep(2)
            except Exception as e:
                continue
            # 京东价
            try:
                price = wait.until(EC.presence_of_element_located((By.XPATH, '//div[contains(@class,"summary")]//span[contains(@class, "J-p-%s")]' % sku))).text
            except Exception as e:
                price = '0.00'
            # 机型
            try:
                model = browser.find_element(By.XPATH, '//*[@id="detail"]//dt[contains(text(),"机型")]/following-sibling::dd[1]').text
                if '' == model:
                    model = '未获取'
            except Exception as e:
                model = '未获取'
            # 上市日期
            try:
                releaseDateText = browser.find_element(By.XPATH, '//*[@id="detail"]//dt[contains(text(),"上市日期")]/following-sibling::dd[1]').text
                # releaseDateText = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]//dt[contains(text(),"上市日期")]/following-sibling::dd[1]'))).text
                if '' == releaseDateText:
                    releaseDateText = '1970-01-01'
            except Exception as e:
                releaseDateText = '1970-01-01'
            # 入网型号
            try:
                netAccessModelText = browser.find_element(By.XPATH, '//*[@id="detail"]//dt[contains(text(),"入网型号")]/following-sibling::dd[2]').text
                # netAccessModelText = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]//dt[contains(text(),"入网型号")]/following-sibling::dd[2]'))).text
                if '' == netAccessModelText:
                    netAccessModelText = '未获取'
            except Exception as e:
                netAccessModelText = '未获取'
            # 机身内存
            try:
                fuselageMemoryText = browser.find_element(By.XPATH, '//*[@id="detail"]//dt[contains(text(),"机身内存")]/following-sibling::dd[1]').text
                # fuselageMemoryText = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]//dt[contains(text(),"机身内存")]/following-sibling::dd[1]'))).text
                if '' == fuselageMemoryText:
                    fuselageMemoryText = getfuselageMemoryText()
            except Exception as e:
                fuselageMemoryText = getfuselageMemoryText()
            # 运行内存
            try:
                runningMemoryText = browser.find_element(By.XPATH, '//*[@id="detail"]//dt[contains(text(),"运行内存")]/following-sibling::dd[1]').text
                # runningMemoryText = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="detail"]//dt[contains(text(),"运行内存")]/following-sibling::dd[1]'))).text
                if '' == runningMemoryText:
                    runningMemoryText = getrunningMemoryText()
            except Exception as e:
                runningMemoryText = getrunningMemoryText()
            # 写日志
            __logger.info("%s,%s,%s,%s,%s,%s,%s,%s" % (sku, model, price, releaseDateText, netAccessModelText, fuselageMemoryText, runningMemoryText, detailurl.strip()))
            time.sleep(2)

# 退出浏览器
browser.quit()
